/*
 * @Author: AskMeWhy
 * @Date: 2020-12-01 18:05:34
 * @Last Modified by: AskMeWhy
 * @Last Modified time: 2021-01-05 15:52:42
 */
import FormMixinBase from "./form.mixin.base";
import Component from "vue-class-component";
import { State } from "vuex-class";
@Component
export default class FormMixin extends FormMixinBase {
    @State(state => state.authArea) authArea: any;
    // 当前用户所处层级菜单
    get client() {
        return this.$getUserLevel(this.authArea && this.authArea.orgCategoryId).name;
    }

    get _isTownAdminPage() {
        return this.$route.name === "TownAdminModuleManageForm";
    }

    mountFormRouteBeforeLeaveInit = false;
    needInterceptRouter = false;
    templateType = "";
    key = 0;

    beforeRouteLeave(to: any, from: any, next: any) {
        if (!this.needInterceptRouter) {
            next();
        } else {
            const timer = setTimeout(() => {
                this.$confirm("您有内容未保存，确认离开？", "提示")
                    .then(() => {
                        clearTimeout(timer);
                        this.resetInterceptRouter();
                        next();
                    })
                    .catch(() => {
                        clearTimeout(timer);
                        next(false);
                    });
            }, 60);
        }
    }

    mounted() {
        const { type }: any = this.$route.query;
        this.templateType = type;
        if (!this.mountFormRouteBeforeLeaveInit) {
            this.setMountFormRouteBeforeLeaveInit(true);
        }
    }

    activated() {
        if (!this.mountFormRouteBeforeLeaveInit) {
            this.setMountFormRouteBeforeLeaveInit(true);
        }
    }

    deactivated() {
        this.setMountFormRouteBeforeLeaveInit(false);
    }

    goBack(params?: any) {
        this.key++;
        if (params && ((params.path && typeof params.path === "string") || params.name)) {
            this.$router.push(params, () => {
                console.log("success");
            });
        } else {
            this.$router.back();
        }
    }

    setMountFormRouteBeforeLeaveInit(val: any) {
        this.mountFormRouteBeforeLeaveInit = val;
        if (this.mountFormRouteBeforeLeaveInit) {
            this.mountFormRouteBeforeLeave();
        }
    }

    mountFormRouteBeforeLeave() {
        const _validateList: any[] = ["ModuleManageForm"];
        const _validateSource = this.$route.name;
        const _validateIndex = _validateList.includes(_validateSource);
        if (!_validateIndex) {
            return;
        }
        const unWatch = this.$watch(
            "model",
            () => {
                this.needInterceptRouter = true;
            },
            {
                deep: true
            }
        );
        this.$on("hook:beforeDestroy", () => {
            unWatch();
        });
    }

    resetInterceptRouter(callback?: any) {
        this.$nextTick(() => {
            this.needInterceptRouter = false;
            callback && callback();
        });
    }
}
